****TABLE 6****

cd "$main_dir/output/tables/main"

*********************************************************************************************************************************************************************
*********************************************************************************************************************************************************************
*COLUMNS 1-2 - GLOBAL SAMPLE (HARMONIZED DATASET)********************************************************************************************************************
*********************************************************************************************************************************************************************
*********************************************************************************************************************************************************************

global basic 	sex marital religion edu

//--------------------------------------------------------
//Read in relevant datasets
//--------------------------------------------------------

clear
use "$data_dir/clean/harmonized_dataset.dta"

merge m:1 ccode survey_year yob using "$data_dir/raw/lifeexp_variables_data/growth_rate_mpd_var_exp.dta", nogen keepusing(total_growth_rate_obs yob_ln_gdppc)
merge m:1 ccode survey_year yob using "$data_dir/raw/lifeexp_variables_data/growth_mpd_var_lambda_exp.dta", nogen keepusing(wgrowth_rate_neg2 wgrowth_rate_neg1 wgrowth_rate_neg0_5 wgrowth_rate_0 wgrowth_rate_0_5 wgrowth_rate_1 wgrowth_rate_2)

fegen subregion_yob = group(subregion yob) if yob!=-99
fegen svy_survey_year = group(survey_year survey)

cap drop sample
gen sample = 1 if born_country!=0 & total_growth_rate_obs>=0.9*(age-1) & total_growth_rate_obs!=.

//--------------------------------------------------------
//Create Table 6 (Columns 1-2)
//--------------------------------------------------------

matrix grid = J(7, 4, .)
	matrix coln grid = rss beta lcl ucl
	matrix rown grid = "-2" "-1" "-0.5" "0" "0.5" "1" "2"

local i = 0
local varlist wgrowth_rate_neg2 wgrowth_rate_neg1 wgrowth_rate_neg0_5 wgrowth_rate_0 wgrowth_rate_0_5 wgrowth_rate_1 wgrowth_rate_2
foreach var of local varlist {
	local ++i
	
	qui reghdfe trust_govt `var' yob_ln_gdppc if sample==1, absorb(ccode subregion_yob svy_survey_year age $basic) cluster(ccode) level(95)
	
	cap drop z*
	local list trust_govt `var' yob_ln_gdppc
	foreach x of local list {
		egen z`x' = std(`x') if e(sample)==1
	}
	
	reghdfe ztrust_govt z`var' zyob_ln_gdppc if e(sample)==1, absorb(ccode subregion_yob svy_survey_year age $basic) cluster(ccode)	
	mat grid[`i', 1] = e(rss)
	mat grid[`i', 2] = _b[z`var']
	mat grid[`i', 3] = r(table)[5,1]
	mat grid[`i', 4] = r(table)[6,1]
	
	di "Model `var' done"
}

matlist grid

svmat double grid, names(col)
drop if rss==.
keep rss beta lcl ucl
export delimited using "table_6_column_1_2.csv", replace


*****************************************************************************************************************************************************************
*****************************************************************************************************************************************************************
*COLUMNS 3-4 - US SAMPLE (ANES)**********************************************************************************************************************************
*****************************************************************************************************************************************************************
*****************************************************************************************************************************************************************

global basic 	sex marital religion edu ethnicity birth_state
global extra	employment_status occupation income urban
global time 	survey_year age generation
global polit    party partisan

//--------------------------------------------------------
//Read in relevant datasets
//--------------------------------------------------------

clear
use "$data_dir/clean/anes_final.dta"

egen scode_survey_year = group(state_code survey_year)

//--------------------------------------------------------
//Create Table 6 (Columns 3-4)
//--------------------------------------------------------

matrix grid = J(7, 4, .)
	matrix coln grid = rss beta lcl ucl
	matrix rown grid = "-2" "-1" "-0.5" "0" "0.5" "1" "2"

local i = 0
local varlist wgrowth_rate_neg2 wgrowth_rate_neg1 wgrowth_rate_neg0_5 wgrowth_rate_0 wgrowth_rate_0_5 wgrowth_rate_1 wgrowth_rate_2
foreach var of local varlist {
	local ++i
	
	qui reghdfe trust_fed_govt `var' yob_ln_gdppc, absorb(state_code $time $basic $extra $polit) cluster(yob)
	
	cap drop z*
	local list trust_fed_govt `var' yob_ln_gdppc
	foreach x of local list {
		egen z`x' = std(`x') if e(sample)==1
	}
	
	reghdfe ztrust_fed_govt z`var' zyob_ln_gdppc if e(sample)==1, absorb(state_code $time $basic $extra $polit) cluster(yob)	
	mat grid[`i', 1] = e(rss)
	mat grid[`i', 2] = _b[z`var']
	mat grid[`i', 3] = r(table)[5,1]
	mat grid[`i', 4] = r(table)[6,1]
	
	di "Model `var' done"
}

matlist grid

preserve
svmat double grid, names(col)
drop if rss==.
keep rss beta lcl ucl
export delimited using "table_6_column_3_4.csv", replace
restore


*******************************************************************************************************************************************************************
*******************************************************************************************************************************************************************
*COLUMNS 5-6 - SWISS SAMPLE (SHP)**********************************************************************************************************************************
*******************************************************************************************************************************************************************
*******************************************************************************************************************************************************************

global basic 	marital edu
global econ	    employment_status occupation income imputed_income
global design 	survey_year age language
global polit    ideology party_member

//--------------------------------------------------------
//Read in relevant datasets
//--------------------------------------------------------

clear
use "$data_dir/clean/shp_final_panel.dta"

xtset idpers survey_year

gen dummy = 1 if trust_fed_govt_med!=.
bys idpers: egen total_obs = total(dummy)

cap drop sample
gen sample = 1 if live_switz==1 & total_obs>4 & total_obs!=. & age>=15 & age!=.

//--------------------------------------------------------
//Create Table 6 (Columns 5-6)
//--------------------------------------------------------

matrix grid = J(7, 4, .)
	matrix coln grid = rss beta lcl ucl
	matrix rown grid = "-2" "-1" "-0.5" "0" "0.5" "1" "2"

local i = 0
local varlist wgrowth_rate_neg2 wgrowth_rate_neg1 wgrowth_rate_neg0_5 wgrowth_rate_0 wgrowth_rate_0_5 wgrowth_rate_1 wgrowth_rate_2
foreach var of local varlist {
	local ++i
	
	qui reghdfe trust_fed_govt_med `var' if sample==1, absorb(canton $basic $design $econ $polit idpers) cluster(idhous yob)
	
	cap drop z*
	local list trust_fed_govt_med `var'
	foreach x of local list {
		egen z`x' = std(`x') if e(sample)==1
	}
	
	qui reghdfe ztrust_fed_govt_med z`var' if e(sample)==1, absorb(canton $basic $design $econ $polit idpers) cluster(idhous yob)	
	mat grid[`i', 1] = e(rss)
	mat grid[`i', 2] = _b[z`var']
	mat grid[`i', 3] = r(table)[5,1]
	mat grid[`i', 4] = r(table)[6,1]
	
	di "Model `var' done"
}

matlist grid

svmat double grid, names(col)
drop if rss==.
keep rss beta lcl ucl
export delimited using "table_6_column_5_6.csv", replace

